const AnnouncementTag = require('../model').AnnouncementTag

exports.create = function(name) {
  return AnnouncementTag.create({ name })
}

exports.remove = function(tagId) {
  return AnnouncementTag.findByIdAndRemove(tagId)
}

exports.isEmpty = async function(tagId) {
  return AnnouncementTag.findOne({
    _id: tagId,
    list: { $size: 0 }
  })
}

exports.updateName = function(tagId, name) {
  return AnnouncementTag.findOneAndUpdate({
    _id: tagId
  }, { name }, { new: true })
}

exports.removeAnnFromListInTag = function(annId, tagId) {
  return AnnouncementTag.findOneAndUpdate({
    _id: tagId
  }, {
    $pull: { list: annId }
  })
}

exports.addAnnToTagList = function(annId, tagId) {
  return AnnouncementTag.findOneAndUpdate({
    _id: tagId
  }, {
    $push: { list: annId }
  })
}
